/**
 * 对应的PCB:WiFi-Switch-iot.zip
 */


#include "taskHandle/TaskHandle.h"

/**
 * 开启一个线程来轮询触摸
 */
Ticker ticker;

/**
 * 当前时间
 */
unsigned long nowTime = 0;

void setup() {
    // 初始化串口波特率
    Serial.begin(115200);
    // 初始化引脚
    initPin();
    // 初始化触摸按键
    initTouchButton();
    // 读取配置文件
    readConfigType();
    // 连接wifi
    connectWiFi();
    // 轮询触摸按键
    ticker.attach_ms(1, touchCheck);
}

unsigned long blinkTime = 0;
unsigned long blinkTimeNotWiFi = 0;
unsigned long sendDataTime = 0;

unsigned long webTime = 0;

void loop() {
    nowTime = millis();
    switch (getWorkMode()) {
        case WORK_MODE_UPDATE:
            delay(10);
            break;
        case WORK_MODE_RESTART:  // 重启
            EspClass::restart();
            break;
        case WORK_MODE_SWITCH_ONE: // 触摸开关1
            switchHandle(WORK_MODE_SWITCH_ONE);
            break;
        case WORK_MODE_SWITCH_TOW: // 触摸开关2
            switchHandle(WORK_MODE_SWITCH_TOW);
            break;
        case WORK_MODE_SWITCH_THREE: // 触摸开关3
            switchHandle(WORK_MODE_SWITCH_THREE);
            break;
        case WORK_MODE_SET_WIFI_CONFIG: // 配网
            WiFi.beginSmartConfig();
            setWorkMode(WORK_MODE_SET_WIFI_CONFIG_WAIT);
            break;
        case WORK_MODE_SET_WIFI_CONFIG_WAIT: // 配网等待
            smartConfigWait();
            break;
        case WORK_MODE_SET_WIFI_WEB_CONFIG: // ap配网
            initSoftAP();
            setWorkMode(WORK_MODE_SET_WIFI_WEB_CONFIG_WAIT);
            break;
        case WORK_MODE_SET_WIFI_WEB_CONFIG_WAIT:  // ap配网等待
            serverLoop();
            if(millis() - webTime >= 2000){
                ledHandle();
                webTime = millis();
            }
            break;
        case SEND_DATA_TO_SERVER: // 发送数据到服务器
            publishMQTT();
            sendDataTime = nowTime;
            break;
        default:
            if (isAutoWiFi) {
                // 判断是否连接wifi
                if (WiFi.status() != WL_CONNECTED)
                    connectWiFi();
                // 已连接
                if (WiFi.status() == WL_CONNECTED) {
                    // 板载LDE闪烁
                    if (nowTime - blinkTime >= LDE_FLASHING_TIME) {
                        ledHandle();
                        blinkTime = nowTime;
                    }
                    // 判断mqtt是否已连接
                    if (!mqttClient.connected()) {
                        connectMQTT();
                    } else {
                        // 处理mqtt接收数据
                        mqttClient.loop();
                        // 到指定时间发送数据到服务器
                        if (nowTime - sendDataTime >= SEND_DATA_TO_SERVER_TIME) {
                            setWorkMode(SEND_DATA_TO_SERVER);
                        }
                    }
                }
            } else {
                // 不联网工作
                if (nowTime - blinkTimeNotWiFi >= LED_FLASHING_NOT_WIFI_TIME) {
                    blinkTimeNotWiFi = nowTime;
                    ledHandle();
                }
            }
            break;
    }
}